<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
use tcpdf\tcpdf;
class Orders extends Base{
    public function orders_list(){
        $keyword=input('get.keyword');
        $state=input('get.state');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['a.time', '>', $fromtime],
            ['a.time', '<', $totime]
        ];
        if($state==0){
            array_push($map,['a.state','<',3]);
        }else{
            array_push($map,['a.state','=',$state]);
        }
        $list=Db::name('orders')->alias('a')->join('users b', 'a.openid = b.openid','LEFT')->field('a.*,b.nickname,b.avatar')->where($map)->where('a.orderno|b.nickname','like',"%{$keyword}%")->order('a.id desc')->paginate(10);
        $lists=Db::name('orders')->alias('a')->join('users b', 'a.openid = b.openid','LEFT')->field('a.*,b.nickname,b.avatar')->where($map)->where('a.orderno|b.nickname','like',"%{$keyword}%")->count();

        $allmoneys=Db::name('orders')->alias('a')->join('users b', 'a.openid = b.openid','LEFT')->field('a.*,b.nickname,b.avatar')->where('a.orderno|b.nickname','like',"%{$keyword}%")->where('a.time', '>', $fromtime)->where('a.time', '<', $totime)->where('a.state',2)->sum('payfee');
        $this->assign('allmoneys',$allmoneys);
        
        $this->assign('keyword',input('get.keyword'));
        $this->assign('state',$state);
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
  	public function export_order() {
        $result=Db::name('orders')->select();
        foreach ($result as $key => $value) {
            $result[$key]['time']=date('Y-m-d H:i:s',$result[$key]['time']);
            $result[$key]['uptime']=date('Y-m-d H:i:s',$result[$key]['uptime']);
            $result[$key]['paytime']=date('Y-m-d H:i:s',$result[$key]['paytime']);
        }
        $xlsCell  = array(
            array('id','ID'),
            array('openid','Openid'),
            array('orderno','订单号'),
            array('payfee','支付金额'),
            array('remark','备注'),
            array('state','状态(1:待支付;2:已支付'),
            array('time','下单时间'),
            array('uptime','更新时间'),
            array('paytime','付款时间')
        );
        $this->exportexcel('订单表',$xlsCell,$result);
    }

    public static function exportexcel($expTitle, $expCellName, $expTableData) {
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $xlsTitle;//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);

        $objPHPExcel = new \PHPExcel();
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
        // Miscellaneous glyphs, UTF-8
        for ($i = 0; $i < $cellNum; $i++) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] .'1', $expCellName[$i][1]);
        }
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < $cellNum; $j++) {
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 2), ' '.$expTableData[$i][$expCellName[$j][0]]);
            }
        }
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
}
